﻿namespace GoogleMusicDesktop
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class SearchBox : UserControl
    {
        private string _GrayText = "搜索";
        private string _Text = string.Empty;
        private PictureBox btnSearch;
        private Label lblBack;
        private TextBox txtKey;
        private Component components = null;

        public SearchBox()
        {
            this.InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.OnClick(e);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(SearchBox));
            this.btnSearch = new PictureBox();
            this.txtKey = new TextBox();
            this.lblBack = new Label();
            ((ISupportInitialize) this.btnSearch).BeginInit();
            base.SuspendLayout();
            this.btnSearch.Anchor = AnchorStyles.Right;
            this.btnSearch.BackColor = Color.Transparent;
            //this.btnSearch.Image = (Image) manager.GetObject("btnSearch.Image");
            this.btnSearch.Location = new Point(160, 0);
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Size = new Size(0x18, 0x16);
            this.btnSearch.SizeMode = PictureBoxSizeMode.CenterImage;
            this.btnSearch.TabIndex = 4;
            this.btnSearch.TabStop = false;
            this.btnSearch.Click += new EventHandler(this.btnSearch_Click);
            this.txtKey.Anchor = AnchorStyles.Right | AnchorStyles.Left;
            this.txtKey.BorderStyle = BorderStyle.None;
            this.txtKey.Font = new Font("微软雅黑", 9f, FontStyle.Italic, GraphicsUnit.Point, 0x86);
            this.txtKey.ForeColor = Color.DarkGray;
            this.txtKey.Location = new Point(5, 3);
            this.txtKey.Name = "txtKey";
            this.txtKey.Size = new Size(0x9a, 0x10);
            this.txtKey.TabIndex = 2;
            this.txtKey.Text = "搜索";
            this.txtKey.TextChanged += new EventHandler(this.txtKey_TextChanged);
            this.txtKey.KeyDown += new KeyEventHandler(this.txtKey_KeyDown);
            this.txtKey.Leave += new EventHandler(this.txtKey_Leave);
            this.txtKey.Enter += new EventHandler(this.txtKey_Enter);
            this.lblBack.Dock = DockStyle.Fill;
            this.lblBack.Location = new Point(0, 0);
            this.lblBack.Name = "lblBack";
            this.lblBack.Size = new Size(0xb8, 0x18);
            this.lblBack.TabIndex = 5;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.White;
            base.BorderStyle = BorderStyle.FixedSingle;
            base.Controls.Add(this.btnSearch);
            base.Controls.Add(this.txtKey);
            base.Controls.Add(this.lblBack);
            base.Name = "SearchBox";
            base.Size = new Size(0xb8, 0x18);
            base.Load += new EventHandler(this.SearchBox_Load);
            base.FontChanged += new EventHandler(this.SearchBox_FontChanged);
            ((ISupportInitialize) this.btnSearch).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void SearchBox_FontChanged(object sender, EventArgs e)
        {
            this.txtKey.Font = this.Font;
        }

        private void SearchBox_Load(object sender, EventArgs e)
        {
            this.txtKey.Text = this.GrayText;
        }

        private void txtKey_Enter(object sender, EventArgs e)
        {
            this.txtKey.ForeColor = Color.Black;
            Font font = new Font(this.txtKey.Font, FontStyle.Regular);
            this.txtKey.Font = font;
            if (string.IsNullOrEmpty(this.Text))
            {
                this.txtKey.Text = string.Empty;
            }
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Return)
            {
                this.OnClick(e);
            }
        }

        private void txtKey_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                this.txtKey.ForeColor = Color.DarkGray;
                Font font = new Font(this.txtKey.Font, FontStyle.Italic);
                this.txtKey.Font = font;
                this.txtKey.Text = this.GrayText;
            }
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            if (this.txtKey.ForeColor == Color.Black)
            {
                this._Text = this.txtKey.Text;
            }
        }

        public string GrayText
        {
            get
            {
                return this._GrayText;
            }
            set
            {
                this._GrayText = value;
            }
        }

        public override string Text
        {
            get
            {
                return this._Text;
            }
            set
            {
                this._Text = value;
            }
        }
    }
}

